package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.auth;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.*;
import com.xiaoxu.intranetweb.util.SzResult;
import org.apache.tomcat.util.codec.binary.Base64;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 用户信息业务层
 *
 * @author xiaoxu
 * @version 1.0
 * @date 2023/1/13 15:00
 * @since 1.0
 */
@Component
public class UserInfoBu {
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ISysRoleInfoFunctionService iSysRoleInfoFunctionService;
    @Autowired
    private ISysRoleInfoFunctionWebService iSysRoleInfoFunctionWebService;
    @Autowired
    private ISysUserPlantConnService iSysUserPlantConnService;
    @Autowired
    private ITPlantsService iTPlantsService;


    public SzResult getUserInfo(String token) {
        QueryWrapper<SysUser> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.select("ID", "TRUE_NAME", "USER_NAME", "PHONE", "APP_AUTHORITY1", "APP_AUTHORITY2");
        queryWrapper1.eq("ID", StpUtil.getLoginIdByToken(token));
        SysUser sysUser1 = iSysUserService.getOne(queryWrapper1);
        String role = sysUser1.getAppAuthority1();
        String[] routes = String.valueOf(role).split("");
        Map<String, String> roles = new HashMap<>();
        List<Integer> list = IntStream.range(0, routes.length)
                .filter(i -> routes[i].equals("1"))
                .boxed()
                .collect(Collectors.toList());

        list.forEach(i -> {
            QueryWrapper<SysRoleInfoFunction> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("WEB_FUNCTION_NUMBER", i);
            SysRoleInfoFunction sysRoleInfoFunction = iSysRoleInfoFunctionService.getOne(queryWrapper2);
            if (sysRoleInfoFunction != null) {
                roles.put(sysRoleInfoFunction.getWebFunctionName(), sysRoleInfoFunction.getWebFunctionName());
            }
        });

        Map<String, Object> map = new HashMap<>();
        map.put("roles", roles);
        map.put("userId", sysUser1.getId());
        map.put("username", sysUser1.getUserName());
        map.put("realName", sysUser1.getTrueName());
        String appAuthority2 = sysUser1.getAppAuthority2();
        String[] codes = String.valueOf(appAuthority2).split("");
        Map<String, String> WebRoles = new HashMap<>();
        Map<String, String> PageRoles = new HashMap<>();
        List<Integer> list2 = IntStream.range(0, codes.length)
                .filter(i -> codes[i].equals("1"))
                .boxed()
                .collect(Collectors.toList());
        list2.forEach(i -> {
            QueryWrapper<SysRoleInfoFunctionWeb> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("WEB_FUNCTION_NUMBER", i);
            SysRoleInfoFunctionWeb sysRoleInfoFunctionWeb = iSysRoleInfoFunctionWebService.getOne(queryWrapper2);
            if (sysRoleInfoFunctionWeb != null) {
                WebRoles.put(sysRoleInfoFunctionWeb.getWebFunctionName(), sysRoleInfoFunctionWeb.getWebFunctionExplain());
            }
        });
        list2.forEach(i -> {
            QueryWrapper<SysRoleInfoFunctionWeb> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("WEB_FUNCTION_NUMBER", i);
            queryWrapper2.eq("PAGE_OR_FUNCTION", 1);
            queryWrapper2.and(wrapper -> wrapper.isNull("WEB_FUNCTION_DEL_FLAG").or().eq("WEB_FUNCTION_DEL_FLAG", "0"));
            SysRoleInfoFunctionWeb sysRoleInfoFunctionWeb = iSysRoleInfoFunctionWebService.getOne(queryWrapper2);
            if (sysRoleInfoFunctionWeb != null) {
                PageRoles.put(sysRoleInfoFunctionWeb.getWebFunctionName(), sysRoleInfoFunctionWeb.getWebFunctionExplain());
            }
        });


        map.put("WebRoles", WebRoles);
        map.put("PageRoles", PageRoles);
        Map<String, Integer> generalColumnsMap = new HashMap<>();
        generalColumnsMap.put("dashboard", 1);
        generalColumnsMap.put("approval", 2);
        generalColumnsMap.put("car", 3);
        generalColumnsMap.put("compete", 4);
        generalColumnsMap.put("customer", 5);
        generalColumnsMap.put("factory", 6);
        generalColumnsMap.put("goods", 7);
        generalColumnsMap.put("iccard", 8);
        generalColumnsMap.put("money", 9);
        generalColumnsMap.put("report", 10);
        generalColumnsMap.put("sale", 11);
        generalColumnsMap.put("sitelive", 12);
        generalColumnsMap.put("statistic", 13);
        generalColumnsMap.put("system", 14);
        generalColumnsMap.put("user", 15);

        Map<String, Integer> generalColumnsMap2 = new HashMap<>();
        list2.forEach(i -> {
            QueryWrapper<SysRoleInfoFunctionWeb> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("WEB_FUNCTION_NUMBER", i);
            queryWrapper3.eq("PAGE_OR_FUNCTION", 1);
            SysRoleInfoFunctionWeb sysRoleInfoFunctionWeb = iSysRoleInfoFunctionWebService.getOne(queryWrapper3);
            if (sysRoleInfoFunctionWeb != null) {
                generalColumnsMap.forEach((k, v) -> {
                    if (k.equals(sysRoleInfoFunctionWeb.getWebFunctionBelongPage())) {
                        generalColumnsMap2.put(k, v);
                    }
                });
            }
        });

        List<Map.Entry<String, Integer>> list3 = new ArrayList<>(generalColumnsMap2.entrySet());
        list3.sort(Comparator.comparing(Map.Entry::getValue)); // 根据value排序
        Map<String, Integer> generalColumnsMap3 = new LinkedHashMap<>();
        for (Map.Entry<String, Integer> entry : list3) {
            generalColumnsMap3.put(entry.getKey(), entry.getValue());
        }
        map.put("generalColumnsMap", generalColumnsMap3);
        //            Resource resource = new ClassPathResource("static/Icon/defaultAvatar.png");
//            File file = resource.getFile();
//            FileInputStream inputStream = new FileInputStream(file);
//            byte[] bytes = new byte[(int) file.length()];
//            inputStream.read(bytes);
//            String base64 = Base64.encodeBase64String(bytes);
//            inputStream.close();
        map.put("avatar", "");
        map.put("desc", "状态正常");
        SzResult szResult = new SzResult();
        szResult.setCode(200);
        szResult.setResult(map);
        return szResult;
    }
}
